package manage.action;

import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.html.VueListResult;
import m.html.element.attr.ButtonType;
import m.html.list.ListButton;
import m.html.list.ListCount;
import m.html.list.SuccessMethod;
import m.system.cache.CacheUtil;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.AdminAssist;
import manage.model.SystemInfo;
import manage.service.AdminAssistService;
import manage.service.SystemInfoService;
import manage.util.CaptchaUtil;

@ActionMeta(name="manage/adminAssist")
public class AdminAssistAction extends StatusAction {

	private AdminAssist model;
	private String imageCode;
	private String password;
	/**
	 * 外协登录
	 */
	public JSONMessage doLogin(){
		JSONMessage message=new JSONMessage();
		try {
			SystemInfo si=getService(SystemInfoService.class).getUniqueModel();
			if(!"Y".equals(si.getAssistLogin())){
				throw new MException(this.getClass(),"外协登录未开启");
			}
			CaptchaUtil.verifyCaptcha(getIpAddress()+getSessionCookie(), imageCode);
			model.setPassword(StringUtil.toMD5(model.getPassword()));
			model=getService(AdminAssistService.class).loginVerification(model);
			setSessionAssist(model);
			getService(AdminAssistService.class).updateLastInfo(model, getIpAddress());
			message.push("model", model.getAdmin());
			message.push("assist",model);
			fillJSONResult(message,true,"登录成功");
			CaptchaUtil.clearMastVerify(getIpAddress()+getSessionCookie());
			CaptchaUtil.clearCode(getIpAddress()+getSessionCookie());
		} catch (Exception e) {
			CaptchaUtil.setMastVerify(getIpAddress()+getSessionCookie());
			fillJSONResult(message,false,e);
		}
		message.push("codeVerify", CaptchaUtil.isMastVerify(getIpAddress()+getSessionCookie()));
		return message;
	}
	/**
	 * 保存
	 * 
	 */
	public JSONMessage doSave(){
		JSONMessage result=new JSONMessage();
		try {
			model.setAdmin(getSessionAdmin());
			if(null!=getSessionAssist()){
				throw new MException(this.getClass(),"外协登录不能保存");
			}
			String msg=getService(AdminAssistService.class).save(model,password);
			CacheUtil.clear(AdminAssist.class, model.getOid());
			fillJSONResult(result,true,msg);
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public VueResult toEdit() throws Exception {
		if(null==model) {
			model=new AdminAssist();
		}else if(!StringUtil.isSpace(model.getOid())){
			model= ModelQueryList.getModel(model,"*");
		}
		return new VuePageResult("page/base/editAdminAssist.js")
			.addParam("model",model);
	}
	public VueResult myAdminAssistList() throws Exception{
		VueListResult result=getVueListResult("外协账号列表",AdminAssist.class,"action/manage/adminAssist/myAdminAssistList");
		result.setSubTitle("添加或者编辑外协账号信息。");
		//result.setSelection("oid", false,"record.oid=='1'");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("登录机构", "orgGroup.name",200).setSort(true)
				.setFieldExpression("concat(#{orgGroup.parent.name},'/',#{orgGroup.name})"));
		result.addCol(toTextColumn("用户名", "username").setSort(true));
		result.addCol(toTextColumn("真实姓名", "realname",true));
		result.addCol(toDateTimeColumn("最后登录时间", "lastLoginTime",160,true).setSort(true));
		result.addCol(toNumberColumn("登录次数", "loginCount").setNumberFormat("#,##0").setCount(ListCount.SUM));
		result.addCol(toStatusColumn(true));
		result.addCol(toSpaceColumn("操作",
			ListButton.modal("修改", ButtonType.PRIMARY,"action/manage/adminAssist/toEdit",600, SuccessMethod.REFRESH)
				.addParam("model.oid","oid")
		));
		result.addButton(
			ListButton.modal("添加",ButtonType.PRIMARY,"action/manage/adminAssist/toEdit",600,SuccessMethod.REFRESH)
				.addQueryParam("model.adminGroup.oid","adminGroupOid")
		);
		result.addCondition(QueryCondition.eq("admin.oid",getSessionAdminOid()));
		return result;
	}

	@Override
	public AdminAssist getModel() {
		return model;
	}

	public void setModel(AdminAssist model) {
		this.model = model;
	}

	@Override
	public String[] getStatusPower() {
		return new String[]{};
	}

	public String getImageCode() {
		return imageCode;
	}

	public void setImageCode(String imageCode) {
		this.imageCode = imageCode;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
